/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */ 
package de.rockon.fuzzy.controller.gui.ruleeditor;

import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;

public class TableBooleanCellRenderer extends DefaultTableCellRenderer {
	private static final long serialVersionUID = 4533992501699019318L;
	private Border border = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK);

	@Override
	public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
			final boolean hasFocus, final int row, final int column) {
		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

		final JCheckBox box = new JCheckBox();
		box.setBackground(super.getBackground());
		if (value != null) {
			boolean enabled = ((Boolean) value).booleanValue();
			String active = enabled ? "active" : "disabled";
			String s = String.format("%5d", (row + 1));
			box.setToolTipText("<html>Rule <b>#" + s + "</b> is <i>" + active + "</i><html>");
		}
		box.setHorizontalAlignment(SwingConstants.CENTER);
		if (value instanceof Boolean) {
			box.setSelected(((Boolean) value).booleanValue());
		}
		if (hasFocus || isSelected) {
			box.setBorder(border);
			box.setBackground(Color.white);
		} else {
			box.setBorder(null);
		}
		// comp.add(box);

		return box;
	}
}